package cn.itcast.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest {
    public static ExecutorService executor = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws Exception{
        System.out.println("main......start.....");
//        CompletableFuture future = CompletableFuture.runAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 10 / 2;
//            System.out.println("运行结果：" + i);
//        },executor);

//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 15 / 5;
//            System.out.println("运行结果：" + i);
//
//            return i;
//        },executor);

        // 处理结果感知
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 15 / 0;
//            System.out.println("运行结果：" + i);
//
//            return i;
//        },executor).whenComplete((res,exception)-> {
//            // 虽然能得到异常信息，但是没发修改返回数据
//            System.out.println("异步任务完成了...结果是："+res+";异常是："+exception);
//        }).exceptionally(throwable -> {
//            // 可以感知异常，同时返回默认值
//            return 10;
//        });

        // 方法执行完成后的处理
//        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 15 / 0;
//            System.out.println("运行结果：" + i);
//
//            return i;
//        },executor).handle((res,thr) -> {
//            if (res!=null) {
//                return res;
//            }
//
//            if (thr!=null) {
//                return 0;
//            }
//
//            return 0;
//        });

//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 15 / 5;
//            System.out.println("运行结果：" + i);
//
//            return i;
//        },executor).thenRun(() -> {
//            System.out.println("任务2启动了...");
//        });

//        CompletableFuture<Void> future = CompletableFuture.supplyAsync(() -> {
//            System.out.println("当前线程：" + Thread.currentThread().getId());
//            int i = 15 / 5;
//            System.out.println("运行结果：" + i);
//
//            return i;
//        },executor).thenAccept(res -> {
//            System.out.println("任务2启动了..."+res);
//        });

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 15 / 5;
            System.out.println("运行结果：" + i);

            return i;
        },executor).thenApplyAsync(res -> {
            System.out.println("任务2启动了..."+res);

            return "Hello 多线程";
        },executor);

        String string = future.get();
        
        System.out.println("main......end....."+string);

//        System.out.println("main......end.....");
    }

    public static void threadTest(String[] args) throws Exception {
        System.out.println("main......start.....");
        /**
         Thread thread = new Thread01();
         thread.start();


         Runable01 runable01 = new Runable01();
         new Thread(runable01).start();

         FutureTask<Integer> futureTask = new FutureTask<>(new Callable01());
         new Thread(futureTask).start();
         System.out.println("main......end....."+futureTask.get());
         **/


//        service.execute(new Runable01());

//        Future<Integer> submit = service.submit(new Callable01());
//        System.out.println(submit.get());

//        ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 200, 10, TimeUnit.SECONDS, new LinkedBlockingDeque<>(1000000), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());

        System.out.println("main......end.....");
    }

    public static class Thread01 extends Thread {
        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Runable01 implements Runnable {

        @Override
        public void run() {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }
    }

    public static class Callable01 implements Callable<Integer> {

        @Override
        public Integer call() throws Exception {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 9 / 3;
            System.out.println("运行结果：" + i);
            return i;
        }
    }
}
